class Solution {
public:
    vector<vector<int>> matrixReshape(vector<vector<int>>& nums, int r, int c) {
        int m = nums.size(),n = nums[0].size();
        vector<vector<int>> ans(r,vector<int>(c,0));
        if(r*c != m*n)
            return nums;
        int a = 0,b = 0;
        for(int i = 0;i < r;++i){
            for(int j = 0;j < c;++j){
                ans[i][j] = nums[a][b++];
                if(b == n){
                    b = 0;a++;
                }
            }
        }
        return ans;
    }
};